﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Avatar.Model;
using AvatarHelper;
using System.Web.Security;

namespace Avatar.MembershipService
{
    /// <summary>
    /// 用户组信息维护
    /// </summary>
    public static class UserGroupManager
    {
        /// <summary>
        /// 根据用户组ID获取用户组
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
        public static Avatar_Group GetUserGroup(int _id)
        {
            return DBHelper.Get<Avatar_Group>(g => g.ID == _id, "获取用户组信息出错:");
        }
        /// <summary>
        /// 获取用户组列表
        /// </summary>
        /// <returns></returns>
        //public static List<Avatar_Group> GetUserGroup()
        //{
        //    return DBHelper.GetAll<Avatar_Group>("获取用户组出错:");
        //}
        public static Dictionary<string,string> GetUserGroup()
        {
            Dictionary<string, string> _roles = new Dictionary<string, string>();
            
            foreach (var _r in Roles.GetAllRoles())
            {
                _roles.Add(_r.ToString(), _r.ToString());
            }

            return _roles;
        }
        /// <summary>
        /// 创建一个用户组
        /// </summary>
        /// <param name="_name"></param>
        /// <param name="_homePage"></param>
        /// <param name="_public"></param>
        /// <returns></returns>
        public static bool CreateGroup(string _name, string _homePage, bool _public)
        {
            if (string.IsNullOrEmpty(_name)) throw new Exception("姓名不能为空");
            if (string.IsNullOrEmpty(_homePage)) throw new Exception("主页不能为空");
            if (_name.Length > 10) throw new Exception("姓名不能超过10个字符");
            if (_homePage.Length > 50) throw new Exception("主页不能超过50个字符");

            Avatar_Group _group = new Avatar_Group();
            _group.Name = _name;
            _group.HomePage = _homePage;

            return DBHelper.Insert(_group, g => g.Name == _name, "用户组添加失败:");
        }
        /// <summary>
        /// 更新用户组信息
        /// </summary>
        /// <param name="_id"></param>
        /// <param name="_name"></param>
        /// <param name="_homePage"></param>
        /// <param name="_public"></param>
        /// <returns></returns>
        public static bool Update(int _id, string _name, string _homePage, bool _public)
        {
            if (string.IsNullOrEmpty(_name)) throw new Exception("姓名不能为空");
            if (string.IsNullOrEmpty(_homePage)) throw new Exception("主页不能为空");
            if (_name.Length > 10) throw new Exception("姓名不能超过10个字符");
            if (_homePage.Length > 50) throw new Exception("主页不能超过50个字符");

            Avatar_Group _group = GetUserGroup(_id);
            return DBHelper.Update(_group, delegate(Avatar_Group _g)
            {
                _g.Name = _name;
                _g.HomePage = _homePage;
                _g.Public = _public;
            }, "更新用户组信息失败:");
        }
        /// <summary>
        /// 删除用户组
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
        public static bool Delete(int _id)
        {
            Avatar_Group _group = DBHelper.Get<Avatar_Group>(g => g.ID == _id, "获取用户组失败:");
            var _users = DBHelper.GetAll<Avatar_User>(u => u.GroupID == _id, "获取用户组出错");
            bool _ok = false;
            if (_users.Count > 0)
            {
                throw new Exception("有用户组存在于该组之中,不能将其删除.");
            }
            else
            {
                _ok = DBHelper.Delete(_group, "删除用户组出错:");
            }
            return _ok;
        }
        /// <summary>
        /// 获取用户组成员数量
        /// </summary>
        /// <param name="_groupID"></param>
        /// <returns></returns>
        public static int NumberOfMembers(int _groupID)
        {
            return GetUserGroup(_groupID).NumberOfUser;
        }
        /// <summary>
        /// 更新用户组成员数量
        /// </summary>
        /// <param name="_groupID"></param>
        /// <param name="_addOrSubtract"></param>
        /// <returns></returns>
        public static bool NumberOfMembers(int _groupID, int _addOrSubtract)
        {
            bool _ok = false;
            var _group = DBHelper.Get<Avatar_Group>(g => g.ID == _groupID, "获取用户组出错：");
            if (_group != null)
            {
                _ok = DBHelper.Update(_group, delegate(Avatar_Group _g)
                {
                    _g.NumberOfUser = _g.NumberOfUser + _addOrSubtract;
                }, "更新用户组成员数量出错:");
            }
            return _ok;
        }
        /// <summary>
        /// 用户加入某一用户组
        /// </summary>
        /// <param name="_userID"></param>
        /// <param name="_groupID"></param>
        /// <returns></returns>
        public static bool JoinGroup(int _userID, int _groupID)
        {
            bool _ok = false;

            Avatar_User _user = DBHelper.Get<Avatar_User>(u => u.ID == _userID, "获取用户失败");
            if (_user.GroupID != _groupID)
            {
                int _oldGroupID = _user.GroupID;

                _ok = DBHelper.Update(_user, delegate(Avatar_User _u)
                {
                    _u.GroupID = _groupID;
                }, "改变用户所在用户组出错:");

                if (_ok) _ok = NumberOfMembers(_groupID, +1);
                if (_ok) _ok = NumberOfMembers(_oldGroupID, -1);
            }

            return _ok;
        }
        /// <summary>
        /// 用户退出某一用户组(未实现)
        /// </summary>
        /// <param name="_groupID"></param>
        /// <param name="_userID"></param>
        /// <returns></returns>
        public static bool ExitGroup(int _groupID, int _userID)
        {
            throw new NotImplementedException();
        }

    }
}
